package com.zx.tradeclient.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zx.base.lazylist.ImageLoader;
import com.zx.tradeclient.R;
import com.zx.tradeclient.ctrl.index2.BuyInfoActivity;
import com.zx.tradeclient.ctrl.index2.Index2Activity;
import com.zx.tradeclient.ctrl.index2.SupplyInfoActivity;
import com.zx.tradeclient.entity.GoodsInfo;

public class HomePageRankingAdapter extends BaseAdapter {

	private ArrayList<GoodsInfo> list = new ArrayList<GoodsInfo>();
	private ImageLoader imageLoader;
	private LayoutInflater inflater;
	private ViewHolder holder = null;
	private Context context;

	public HomePageRankingAdapter(Context context, ArrayList<GoodsInfo> img,
			ImageLoader imageLoader) {
		this.list = img;
		this.imageLoader = imageLoader;
		this.context = context;
		this.inflater = LayoutInflater.from(context);
	}

	public int getCount() {
		return list.size();
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View view, ViewGroup viewGroup) {

		final GoodsInfo goodsItem = list.get(position);

		if (view == null) {

			view = inflater.inflate(R.layout.goods_item, null);
			holder = new ViewHolder();
			holder.goods_icon = (ImageView) view
					.findViewById(R.id.index2_goods_icon);
			holder.goods_hot = (ImageView) view
					.findViewById(R.id.index2_goods_hot);
			holder.goods_name = (TextView) view
					.findViewById(R.id.index2_goods_name);
			holder.goods_number_type = (TextView) view
					.findViewById(R.id.index2_goods_number_type);
			holder.goods_number = (TextView) view
					.findViewById(R.id.index2_goods_number);
			holder.goods_date = (TextView) view
					.findViewById(R.id.index2_goods_date);
			holder.goods_price = (TextView) view
					.findViewById(R.id.index2_goods_price);
			holder.goods_address = (TextView) view
					.findViewById(R.id.index2_goods_address);
			holder.goods_layout = (LinearLayout) view
					.findViewById(R.id.index2_goods_line);

			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}

		if (goodsItem != null) {
			holder.goods_name.setText(goodsItem.getGoods_name());
			if (goodsItem.getGoods_number_type().equals("00")) {
				holder.goods_number_type.setText(R.string.buy_info_number);
				holder.goods_icon.setBackgroundResource(R.drawable.icon_want);
			} else if (goodsItem.getGoods_number_type().equals("11")) {
				holder.goods_number_type.setText(R.string.supply_info_number);
				holder.goods_icon.setBackgroundResource(R.drawable.icon_supply);
			}

			holder.goods_number.setText(goodsItem.getGoods_number());
			holder.goods_date.setText(goodsItem.getGoods_date());
			holder.goods_price.setText(goodsItem.getGoods_price());
			holder.goods_address.setText(goodsItem.getGoods_site());
			if("1".equals(goodsItem.getGoods_ishot())){
				holder.goods_hot.setVisibility(View.VISIBLE);
			}else{
				holder.goods_hot.setVisibility(View.GONE);
			}
			

			holder.goods_layout.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					if (goodsItem.getGoods_number_type().equals("00")) {
						Intent intent = new Intent(context,
								BuyInfoActivity.class);
						intent.putExtra("productid", goodsItem.getGoods_id());
						((Index2Activity) context).startActivity(intent);
					} else if (goodsItem.getGoods_number_type().equals("11")) {
						Intent intent = new Intent(context,
								SupplyInfoActivity.class);
						intent.putExtra("productid", goodsItem.getGoods_id());
						((Index2Activity) context).startActivity(intent);
					}

				}
			});
		}

		return view;
	}

	private static class ViewHolder {
		public TextView goods_name;
		public TextView goods_number_type;
		public TextView goods_number;
		public TextView goods_date;
		public TextView goods_price;
		public TextView goods_address;
		public LinearLayout goods_layout;
		public ImageView goods_icon;
		public ImageView goods_hot;
	}

}